package com.example.d9_homework.homework01;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        // 需求：有四名学生参加注册会计师考试,表格中记录的是4名学生的考试成绩,请按照成绩将四名学生从小到大进行排名,并将结果展示出来
        // 1.定义一个ArrayList集合 list
        List<Student> list = new ArrayList<>();

        // 2.创建4个学生对象
        Student s1 = new Student(1, "小亮", 99);
        Student s2 = new Student(2, "小勇", 85);
        Student s3 = new Student(3, "小响", 90);
        Student s4 = new Student(4, "小强", 89);

        // 3.将学生对象添加到集合中
        Collections.addAll(list, s1, s2, s3, s4);

        // 使用stream流一步到位
        list.stream().sorted(Comparator.comparingInt(Student::getScore))
                .forEach(student -> System.out.println(student.getId() + "---" + student.getName() + "---" + student.getScore()));

        // 4.对集合进行排序
//        Collections.sort(list, new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getScore() - o2.getScore();
//            }
//        });
        // 使用Lambda简化
        // Collections.sort(list, (o1, o2) -> o1.getScore() - o2.getScore());

        // 使用方法引用简化
        Collections.sort(list, Comparator.comparingInt(Student::getScore));


        // 5.输出集合信息
        for (Student student : list) {
            System.out.println(student.getId() + "---" + student.getName() + "---" + student.getScore());
        }
    }
}
